﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using RkLib.Xml;
using RkLib;
using RkLib.Configuration;
using System.IO;
namespace Ebenalp
{
    public partial class FormMain : Form
    {
        SnGen snGen = null;
        XmlManager xmlMgr = null;
        public FormMain()
        {
            InitializeComponent();
            this.Text = "Ebenalp依本立卷管器产品选型工具 试用版";
            this.Icon = Ebenalp.Properties.Resources.SnTool;
            gVar.InitGVar();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            int i = 0;
            this.rtbInfo.Text = gVar.GetDescription();
#if DEBUG
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "src.xml";
#else
            string filePath = AppDomain.CurrentDomain.BaseDirectory + "abbrdata.bin";
#endif

            if (!File.Exists(filePath))
            {
                ShowMsg("配置文件丢失");
                return;
            }

            xmlMgr = new XmlManager(filePath);
            snGen = new SnGen(xmlMgr);

            InitControl(i++, this.labSel1, this.cbb1);
            InitControl(i++, this.labSel2, this.cbb2);
            InitControl(i++, this.labSel3, this.cbb3);
            InitControl(i++, this.labSel4, this.cbb4);
            InitControl(i++, this.labSel5, this.tb5);
            InitControl(i++, this.labSel6, this.cbb6);
            InitControl(i++, this.labSel7, this.cbb7);
            InitControl(i++, this.labSel8, this.cbb8);
            InitControl(i++, this.labSel9, this.tb9);
            InitControl(i++, this.labSel10, this.cbb10);
            
        }
        private void InitControl(int index, Control controParent, Control controlChild)
        {
            ComboBox cbb;
            int nSel;
            if (controlChild is ComboBox)
            {
                string[] arrSrc;
                string parent = null;
                if (SnGen.ITEM7 == index)
                {
                    arrSrc = snGen.GetSumItems(index);
                    AddComboBoxItems((ComboBox)controParent, arrSrc);
                    parent = arrSrc[0];
                }
                arrSrc = snGen.GetSumItemsName(index, parent);
                AddComboBoxItems((ComboBox)controlChild, arrSrc);
            }
            snGen[index] = new ItemInfo(controParent.Text, controlChild.Text);

            if (controParent is ComboBox)
            {
                cbb = (ComboBox)controParent;
                nSel = Cfg.ReadInt(gVar.CFG_CONTROL,controParent.Name);
                cbb.SelectedIndex = nSel < cbb.Items.Count ? nSel : 0;
            }            
            if (controlChild is ComboBox)
            {
                cbb = (ComboBox)controlChild;
                nSel = Cfg.ReadInt(gVar.CFG_CONTROL, controParent.Text);
                cbb.SelectedIndex = nSel < cbb.Items.Count ? nSel : 0;
            }
            if (controlChild is TextBox)
            {
                controlChild.Text = Cfg.ReadString(gVar.CFG_CONTROL, controParent.Text);
            }
        }

        private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

            ValueChanged(sender as Control);
        }

        private void TextBox_TextChanged(object sender, EventArgs e)
        {
            ValueChanged(sender as Control);
        }
        private void ValueChanged(Control control)
        {
            string str = PublicFunction.GetNumStrFromStr(control.Name);
            int index = Convert.ToInt32(str);
            Control ctr = GetControlByName("labSel" + index);
            snGen.ValueChanged(index-1, ctr.Text, control.Text);

            this.tbSN.Text = snGen.ToString();
            if (control is ComboBox)
            {
                int nSel = ((ComboBox)control).SelectedIndex;
                Cfg.WriteConfig(gVar.CFG_CONTROL, ctr.Text, nSel);
            }
            else
            {
                Cfg.WriteConfig(gVar.CFG_CONTROL, ctr.Text, control.Text);
            }
            string picPath = Cfg.ReadString(gVar.CFG_PIC, this.tbSN.Text);
            if (File.Exists(picPath))
            {
                this.picDis.Load(picPath);
            }
            else
            {
                this.picDis.Image = null;
            }
        }
        private Control GetControlByName(string name)
        {
            foreach (Control ctrl in this.Controls)
            {
                if (ctrl.HasChildren)
                {
                    Control c = GetControlByName(ctrl.Controls,name);
                    if (c != null)
                    {
                        return c;
                    }
                }
                else
                {
                    if (ctrl.Name == name)
                    {
                        return ctrl;
                    }
                }
            }
            return null;
        }
        private Control GetControlByName(Control.ControlCollection cc, string name)
        {
            foreach (Control c in cc)
            {
                if (c.HasChildren)
                {
                    Control ctrl = GetControlByName(c.Controls, name);
                }
                else
                {
                    if (c.Name == name)
                    {
                        return c;
                    }
                }
            }
            return null;
        }
        private void AddComboBoxItems(ComboBox control, string[] arr, bool bClear = true)
        {
            if (bClear)
            {
                control.Items.Clear();
            }
            for (int i = 0; i < arr.Length; i++ )
            {
                control.Items.Add(arr[i]);
            }
        }

        private void labSel7_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] arrSrc = snGen.GetSumItems(SnGen.ITEM7);
            string[] arrDst = snGen.GetSumItems(SnGen.ITEM9);
            ComboBox cbb = (ComboBox)sender;
            string str = PublicFunction.GetNumStrFromStr(cbb.Name);
            int index = Convert.ToInt32(str);
            int idx;
            for (idx = 0; idx < arrSrc.Length;idx++ )
            {
                if (arrSrc[idx] == cbb.Text)
                {
                    this.labSel9.Text = string.Format("{0}、{1}", SnGen.ITEM9 + 1 , arrDst[idx]);
                    this.tb9.Text = Cfg.ReadString(gVar.CFG_CONTROL,this.labSel9.Text);
                }
            }
            arrSrc = snGen.GetSumItemsName(index-1, cbb.Text);
            AddComboBoxItems(this.cbb7, arrSrc);
            int nSel = Cfg.ReadInt(gVar.CFG_CONTROL, cbb.Text);
            this.cbb7.SelectedIndex = nSel<this.cbb7.Items.Count?nSel:0;
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            gVar.Terminate();
        }

        private void menuPictureImport_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "图片文件(*.jpg)|*.jpg";
            ofd.Multiselect = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                snGen.ImportPicture(ofd.FileNames);
            }
        }

        private void menuPictureDeleteAll_Click(object sender, EventArgs e)
        {
            Cfg.DeleteSection(gVar.CFG_PIC);
            this.picDis.Image = null;
        }
        private delegate void dg_ShowMsg(string msg);
        private void ShowMsg(string msg)
        {
            if (this.InvokeRequired)
            {
                dg_ShowMsg method = ShowMsg;
                Invoke(method, msg);
            }
            else
            {
                MessageBox.Show(msg, "提醒");
            }
        }
        
    }
}
